<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/15 0015
 * Time: 12:48
 */
namespace app\api\controller;


use app\common\controller\Api;
use think\Db;
use think\Model;
class Wechatuser extends Api{

    /**
     * 定义一个变量 用于apiGroup 因为不支持直接输入中文
     * @apiDefine wechatuser 微信用户
     */
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    /**
     * @apiGroup wechatuser
     * @apiName  create
     * @apiDescription  保存微信用户信息
     * @api {get} /api/wechatuser/create  第二步保存用户信息
     * @apiparam {string}  user_id      用户id
     * @apiparam {string}  nickName      微信昵称
     * @apiparam {string}  avatarUrl      微信用户头像
     * @apiSuccessExample Success-Response:
     * {"code":1,"msg":"返回成功","time":"1576386140"}
     */
    public function create(){
        $map = array();
        $map['id'] = $this->request->post('user_id');
        $maps['nickName'] = $this->request->post('nickName');
        $maps['avatarUrl'] = $this->request->post('avatarUrl');
        $user_id = Db::name('wechatuser')->where($map)->update($maps);
        $this->success('返回成功');

    }
    /**
     * @apiGroup wechatuser
     * @apiName  code
     * @apiparam {string}  openid      微信openid
     * @api {post} /api/wechatuser/getopenid  第一步获取openid
     * @apiSuccessExample Success-Response:
     * {"code":1,"msg":"返回成功","time":"1576500393","data":{"user_id":3,"openid":"3234234fdfdasdf3454"}}
     */
    public function getopenid(){
        $code = $this->request->post('code');
       // $code = '033C8VoX0mK2YV1HsvpX0id5pX0C8Vob';
        $appid = 'wx1c6c81c374e49064';
        $secret = 'b79d4722271276adcb02cd604f758158';
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code=' . $code . '&grant_type=authorization_code';
        $info = file_get_contents($url);//发送HTTPs请求并获取返回的数据，推荐使用curl

        $json = json_decode($info);//对json数据解码
        $arr = get_object_vars($json);
        if(isset($arr['errcode'])){
            $this->error('获取openid失败');
        }
        $openid = $arr['openid'];
        $data = Db::name('wechatuser')->where('openid',$openid)->find();
        if(!$data) {
            $maps['openid'] = $openid;
            $maps['addtime'] = date('Y-m-d H:i:s', time());
            $user_id = Db::name('wechatuser')->insertGetId($maps);
        }else{
            $user_id = $data['id'];
        }
        $this->success('返回成功',['user_id'=>$user_id,'openid'=>$openid]);
    }

    /**
     * @apiGroup wechatuser
     * @apiName  upload
     * @api {file} /api/wechatuser/upload  上传接口
     * @apiparam {string} file_name      上传文件名
     * @apiSuccessExample Success-Response:
     * {"code":1,"msg":"返回成功","time":"1576386140","data":{"file_name":"upload/22d.jpg"}}
     */
    public function upload()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file_name');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if ($info) {
            // 成功上传后 获取上传信息
            // 输出 jpg
            //  echo $info->getExtension();
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            $path =  'uploads';
            $file_name = $info->getSaveName();

            $this->success('返回成功', ['file_name' => $path.'/'.$file_name]);
            // echo $info->getSaveName();
            // 输出 42a79759f284b767dfcb2a0197904287.jpg
            // echo $info->getFilename();
        } else {
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }

    /**
     * @apiGroup wechatuser
     * @apiName  collection
     * @api {post} /api/wechatuser/collection  添加收藏
     * @apiparam {string} user_id               用户id 从微信添加用户获取
     * @apiparam {string} shop_id                店铺id
     *@apiparam {string} status                状态可选 传1 为取消收藏 不传 status 收藏成功
     * @apiSuccessExample Success-Response:
     *{"code":1,"msg":"返回成功","time":"1576548701","data":{"status":1}} 1 没有收藏 0 收藏成功
     */
    public function collection(){

        $user_id = $this->request->post('user_id');
        $shop_id = $this->request->post('shop_id');
        $map['user_id'] = $user_id;
        $map['shop_id'] = $shop_id;
        $status = $this->request->post('status');
        $data = Db::name('collection')->where($map)->find();
        if($data){
            if($status){
                Db::name('collection')->where('id',$data['id'])->update(['status'=>'1']);
            }else{
                Db::name('collection')->where('id',$data['id'])->update(['status'=>'0']);
            }
        }else{
            $map['addtime'] = date('Y-m-d H:i:s',time());
             Db::name('collection')->insert($map);
        }
        $status = Db::name('collection')->where('id',$data['id'])->find();
        $this->success('返回成功',['status'=>$status['status']]);
    }
    /**
     * @apiGroup wechatuser
     * @apiName  mycollection
     * @api {post} /api/wechatuser/mycollection  我收藏的店铺
     * @apiparam {string} user_id               用户id 从微信添加用户获取
     * @apiparam {string} page       当前页数
     * @apiparam {string} pagenum       一页多少条
     * @apiSuccessExample Success-Response:
     * {"code":1,"msg":"返回成功"}}
     */
    public function myCollection(){
        $map = array();
       $map['user_id'] = $this->request->post('user_id');
        $map['c.status'] = '0';
        $page =  $this->request->post('page');
        $pagenum =  $this->request->post('pagenum');
        $data = Db::name('collection')->field('c.shop_id,shop_name,shop_image,star_class,tags,description')->alias('c')->where($map)->join('__SHOP__ s','c.shop_id = s.id','left')->page($page,$pagenum)->select();
       // $data['count'] = $count;
        if($data){
            $this->success('返回成功',$data);
        }else{
            $this->error('返回失败');
        }

    }

    /**
     * @apiGroup wechatuser
     * @apiName  mycomment
     * @api {post} /api/wechatuser/myComment  我的评论
     * @apiparam {string} user_id               用户id 从微信添加用户获取
     * @apiparam {string} page       当前页数
     * @apiparam {string} pagenum       一页多少条
     * @apiSuccessExample Success-Response:
     * {"code":1,"msg":"返回成功","time":"1576549702","data":[{"shop_name":"测试","shop_id":1,"commnet_id":1,"content":"dfdfd","comment_img":"dfdfd","star":3,"addtime":"2019-12-12 16:10:40"},{"shop_name":"测试33","shop_id":2,"commnet_id":3,"content":"3243234dfdf","comment_img":"3243234dfdf","star":3,"addtime":"2019-12-17 00:00:00"}]}
     */
    public  function myComment(){
        $page =  $this->request->post('page');
        $pagenum =  $this->request->post('pagenum');
        $user_id = $this->request->post('user_id');
        $data = Db::name('shop_comment')->alias('c')->join('__SHOP__ s','c.shop_id=s.id','left')->where('c.user_id',$user_id)
            ->field('shop_name,s.id as shop_id,c.id as commnet_id,content,comment_img,star,c.addtime')->page($page,$pagenum)->select();
			
		    foreach ($data as $k => &$v) {
			if (strpos($v['comment_img'], ',')) {
                $v['comment_img'] = explode(',', $v['comment_img']);
            } else {
                if ($v['comment_img']) {
                    $v['comment_img'] = [$v['comment_img']];
                } else {
                    $v['comment_img'] = [];
                }
            }
			}
			
        $count = Db::name('shop_comment')->alias('c')->join('__SHOP__ s','c.shop_id=s.id','left')->where('c.user_id',$user_id)
            ->field('shop_name,s.id as shop_id,c.id as commnet_id,content,comment_img,star,c.addtime')->count();
     //   $data['count'] = $count;
        if($data){
            $this->success('返回成功',$data);
        }else{
            $this->error('返回失败');
        }
    }

    /**
     * @apiGroup wechatuser
     * @apiName  demycomment
     * @api {post} /api/wechatuser/deMyComment  删除我的评论
     * @apiparam {string} comment_id              我的评论列表的 comment_id
     * @apiSuccessExample Success-Response:
     * {"code":1,"msg":"返回成功","time":"1576549998","data":null}
     *
     */
    public function deMyComment(){
        $comment_id = $this->request->post('comment_id');
        $data = Db::name('shop_comment')->where('id',$comment_id)->delete();
        if($data){
            $this->success('返回成功');
        }else{
            $this->error('返回失败');
        }
    }
}